package agency.tango.materialintro.slice;

import agency.tango.materialintro.ComponentDemoAdapter;
import agency.tango.materialintro.ResourceTable;
import agency.tango.materialintro.DemoView;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.PageSlider;

public class ComponentDemoAbilitySlice extends AbilitySlice {

    private ComponentDemoAdapter adapter;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_component_demo);
        initView();
    }

    private void initView() {
        PageSlider viewPager = (PageSlider) findComponentById(ResourceTable.Id_viewPager);
        viewPager.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int position, float positionOffset, int positionOffsetPixels) {
                int tempPosition = position;
                float tempPositionOffset = positionOffset;
                if (adapter == null) {
                    return;
                }
                float width = (float) viewPager.getWidth();
                int currentPage = viewPager.getCurrentPage();
                float scrollX = 0f;
                if (currentPage >= 0 && currentPage < adapter.getCount()
                        && adapter.getItem(currentPage) != null
                        && adapter.getItem(currentPage).getRootView() != null) {
                    float parentLeft = (float) viewPager.getLocationOnScreen()[0];
                    float itemX = (float) adapter.getItem(currentPage).getRootView().getLocationOnScreen()[0];
                    scrollX = itemX - parentLeft;
                }
                if (width > 0) {
                    if (scrollX <= 0f) {
                        tempPosition = currentPage;
                        tempPositionOffset = Math.abs(scrollX) / Math.abs(width);
                    } else {
                        tempPosition = currentPage - 1;
                        tempPositionOffset = 1 - (Math.abs(scrollX) / Math.abs(width));
                    }
                }
                if (tempPosition >= adapter.getCount()) {
                    tempPosition = adapter.getCount() - 1;
                }
                if (tempPosition < 0) {
                    tempPosition = 0;
                }
                if (tempPositionOffset < 0) {
                    tempPositionOffset = 0;
                }
                if (tempPositionOffset > 1) {
                    tempPositionOffset = 1;
                }
                if (scrollX <= 0f) {
                    adapter.getItem(viewPager.getCurrentPage()).setOffset(tempPositionOffset);
                } else {
                    adapter.getItem(viewPager.getCurrentPage()).setOffset(tempPositionOffset - 1);
                }
            }

            @Override
            public void onPageSlideStateChanged(int i) {

            }

            @Override
            public void onPageChosen(int i) {
                adapter.getItem(viewPager.getCurrentPage()).setOffset(0);
                adapter.getItem(i).setOffset(0);
            }
        });
        adapter = new ComponentDemoAdapter(getContext());
        viewPager.setProvider(adapter);
        adapter.addItem(new DemoView(getContext(), 0));
        adapter.addItem(new DemoView(getContext(), 1));
        adapter.addItem(new DemoView(getContext(), 2));
        adapter.addItem(new DemoView(getContext(), 3));
        adapter.addItem(new DemoView(getContext(), 4));
        adapter.addItem(new DemoView(getContext(), 5));
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
